package lp.polimi.treefinder;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.os.Bundle;
import android.view.View;

/** 
 * After the user selects a tagged leaf from the list, this activity is shown, allowing to access the leaf details (species name, address,
 * date) and the webview showing the insights from Wikipedia (if available).
 */
public class LeafInsightsActivity extends MediaMountedActivity {

	private static final String CURRENTLY_SELECTED_TAB = "selTab";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_leaf_insights);

		//check used layout
		View layout = (View) findViewById(R.id.leaf_insights);
		String screen = (String) layout.getTag();

		
		/* If it's a smartphone, then setup action bar in order to add the tabs (leaf details fragment and
		 * Wikipedia fragment); if it's a tablet, do not setup anything (the structure will be managed by the layout resources).
		 */
		if (screen.equals("small")) {
			ActionBar actionBar = getActionBar();
			actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
			actionBar.setTitle(getIntent().getExtras().getString(XMLHandler.KEY_NAME));

			Tab tab = actionBar.newTab()
					.setText(R.string.data)
					.setTabListener(new TabListener<LeafDataFragment>(
							this, "leaf_data", LeafDataFragment.class));
			actionBar.addTab(tab);

			tab = actionBar.newTab()
					.setText(R.string.wiki)
					.setTabListener(new TabListener<WikipediaFragment>(
							this, "wiki", WikipediaFragment.class));
			actionBar.addTab(tab);

			if (savedInstanceState!=null)
				actionBar.setSelectedNavigationItem(savedInstanceState.getInt(CURRENTLY_SELECTED_TAB));
		}
	}


	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putInt(CURRENTLY_SELECTED_TAB, getActionBar().getSelectedNavigationIndex());
	}
}
